<?

class Admin extends CI_controller {




	function __construct()
	{
	parent::__construct();
	$this->is_logged_in();
	}



	function test() {
		
		
		echo "rollespiller id".$this->uri->segment(4).br();
		echo "karakter id".$this->uri->segment(3);

		/*echo $this->input->post("username");
		echo $this->input->post("rollespiller_fornavn");
		echo $this->input->post("rollespiller_efternavn");
		echo $this->input->post("email_adress");
		echo $this->input->post("admin");	*/
		
		
	}



	function slet_deltager() {
		
		
		$deltager_id = $this->input->post("deltager_id");
		echo $deltager_id;
		$this->load->model("rettighed/administrator");
		$this->administrator->slet_deltager($deltager_id);
	
	}


	

	function pc_to_npc() {
	
		$this->load->model("rettighed/administrator");
	
		//echo "rollespiller id".$this->uri->segment(4).br();
		//echo "karakter id".$this->uri->segment(3);
	
		$rollespiller_id = $this->uri->segment(4).br();
		$pc_id = $this->uri->segment(3);
		
		$this->administrator->delete_pc($pc_id,$rollespiller_id);
		
		redirect("forord");
	
	}

	function create_user() {

		$this->load->model("rettighed/rollespiller");
		$this->load->model("core/opret_model");
	
		$username = $this->input->post("username");
		$rollespiller_fornavn = $this->input->post("rollespiller_fornavn");
		$rollespiller_efternavn =  $this->input->post("rollespiller_efternavn");
		$email_adress = $this->input->post("email_adress");
		$admin = $this->input->post("admin");	
		$aktiv = $this->input->post("aktiv");	
	
		$new_user_id = $this->rollespiller->create_user($username,$rollespiller_fornavn,$rollespiller_efternavn,$email_adress,$admin,$aktiv);


		echo $this->input->post("pc_pick");
		if($this->input->post("pc_pick") == "new") {
			echo "Tildel rolle new";
			$birth = $this->input->post('birth');
			if(!$birth == "check")
			{
                $birthtime = $this->input->post('time');
                $birthminut = $this->input->post('minut');

                if(!empty($birthtime)) {
                    $birth_klokken = $birthtime.":".$birthminut.":00";
                } 
                else { 
                    $birth_klokken = NULL; 
                }
                

				$birthalder = $this->input->post('alder');
				$birthaar = $this->input->post('aar');
				$birthmaaned = $this->input->post('maaned');
				$birthdag = $this->input->post('dag');

				$birthdato = $this->tid_model->til_db($birthalder, $birthaar, $birthmaaned, $birthdag);
     
    		}
			else 
			{
					$birthdato = 0;
			}

			//checker om death er angivet			
			$death = $this->input->post('death');
			if(!$death == "check")
			{

                $deathtime = $this->input->post('time_2');
                $deathminut = $this->input->post('minut_2');

                if(!empty($deathtime)) {
                    $death_klokken = $deathtime.":".$deathminut.":00";
                } 
                else { 
                    $death_klokken = NULL;
                }

				$deathalder = 50; //alder bliver ikke brugt endnu - men skal være defineret som halvtreds for at det virker (god kode).
				$deathaar = $this->input->post('aar_2');
				$deathmaaned = $this->input->post('maaned_2');
				$deathdag = $this->input->post('dag_2');
				$deathdato = $this->tid_model->til_db($deathalder, $deathaar, $deathmaaned, $deathdag);
			}
			else 
			{
					$deathdato = 0;
                    $death_klokken = NULL;
			}

			$ingame = $this->session->userdata("ingame");
		
			$navn = $this->input->post("individ_navn");
			//$birthdato = $this->input->post("pc_pick");
			//$deathdato = $this->input->post("pc_pick");
			//$birth_klokken = $this->input->post("pc_pick");
			//$death_klokken = $this->input->post("pc_pick");
			$over = $this->input->post("over");
			$userid = $this->session->userdata("userid");
			$race = $this->input->post("race");
			$id_rollespiller = $new_user_id;
			$id_kampagne = $ingame['kampagne_id'];
		
			
			$this->opret_model->player_character($navn,$birthdato,$deathdato,$birth_klokken,$death_klokken,$over,$userid,$race, $id_rollespiller, $id_kampagne);


		}

		redirect("forord");

		//echo $user_id;

	
	}



	function opret_ny_pc_eksisterende_deltager() {

			$this->load->model("rettighed/rollespiller");
			$this->load->model("core/opret_model");		
		
			$birth = $this->input->post('birth');
			if(!$birth == "check")
			{
                $birthtime = $this->input->post('time');
                $birthminut = $this->input->post('minut');

                if(!empty($birthtime)) {
                    $birth_klokken = $birthtime.":".$birthminut.":00";
                } 
                else { 
                    $birth_klokken = NULL; 
                }
                

				$birthalder = $this->input->post('alder');
				$birthaar = $this->input->post('aar');
				$birthmaaned = $this->input->post('maaned');
				$birthdag = $this->input->post('dag');

				$birthdato = $this->tid_model->til_db($birthalder, $birthaar, $birthmaaned, $birthdag);
     
    		}
			else 
			{
					$birthdato = 0;
			}

			//checker om death er angivet			
			$death = $this->input->post('death');
			if(!$death == "check")
			{

                $deathtime = $this->input->post('time_2');
                $deathminut = $this->input->post('minut_2');

                if(!empty($deathtime)) {
                    $death_klokken = $deathtime.":".$deathminut.":00";
                } 
                else { 
                    $death_klokken = NULL;
                }

				$deathalder = 50; //alder bliver ikke brugt endnu - men skal være defineret som halvtreds for at det virker (god kode).
				$deathaar = $this->input->post('aar_2');
				$deathmaaned = $this->input->post('maaned_2');
				$deathdag = $this->input->post('dag_2');
				$deathdato = $this->tid_model->til_db($deathalder, $deathaar, $deathmaaned, $deathdag);
			}
			else 
			{
					$deathdato = 0;
                    $death_klokken = NULL;
			}


			$ingame = $this->session->userdata("ingame");
		
			$navn = $this->input->post("individ_navn");
			//$birthdato = $this->input->post("pc_pick");
			//$deathdato = $this->input->post("pc_pick");
			//$birth_klokken = $this->input->post("pc_pick");
			//$death_klokken = $this->input->post("pc_pick");
			$over = $this->input->post("over");
			$userid = $this->session->userdata("userid");
			$race = $this->input->post("race");
			$id_rollespiller = $this->input->post("rollespiller_id");
			$id_kampagne = $ingame['kampagne_id'];
		
			
			$this->opret_model->player_character($navn,$birthdato,$deathdato,$birth_klokken,$death_klokken,$over,$userid,$race, $id_rollespiller, $id_kampagne);		

		redirect("forord");
		
		
	}




	function dashboard()
	{	
        $this->load->library('table');

		$this->load->model('core/kampagne'); 
		$this->load->model('rollespiller_model'); 

		$element = new kampagne();
		$element->find_kampagner();
		$data['kampagner'] = $element->kampagner;
		$data['rollespillere'] = $this->rollespiller_model->find_rollespillere(1);	

	//	print_r($data['deltager']);
		$data['ingame'] = $this->session->userdata('ingame');
		$data['deltager'] = $this->session->userdata('deltager');
		// BOGMÆRKER HENTES IND
		$data['bookmarks'] = $this->rollespiller_model->find_bookmarks($data['deltager']['userid']);

		$this->load->model('core/player_character'); 
		$rollespillere = new player_character();

		$rollespillere->find_alle_rollespillere_karakterer();
		$data['rollespillere_karakterer'] = $rollespillere->alle_rollespillere_karakterer;

		//print_r($data['rollespillere_karakterer']);

		$data['overskrift_left'] = "Administratorens";
		$data['overskrift_right'] = "Deltageroversigt";
	//	$data['underskrift_left'] = "I:5";
	//	$data['underskrift_right'] = "";

		$data['sidehoved_right'] = "admin/sidehoved_right";
		$data['sidehoved_left'] = "almanak/sidehoved_left";

		$indhold_left[] = 'admin/admin_dash';


        if($this->uri->segment(3) == "kampagner") {
		    $indhold_right[] = 'admin/kampagner';

        }
		else {
		    $indhold_right[] = 'admin/rollespiller_liste';
		}
		
		$data['content_left'] = $indhold_left;
		$data['content_right'] = $indhold_right;
		$this->load->view('includes/templates/bogpaapult',$data); // 2 * $indhold

    }


	function beskrivelser()
	{	

		$data['vis'] = $this->uri->segment(3,0);
		$data['id_beskrivelse'] = $this->uri->segment(4,0);
		$data['navigation'] = $this->uri->segment(5,0);


		//modeller
		$this->load->model('indhold/element_beskrivelse');
		$this->load->model('indhold/ingame_beskrivelse');
		$this->load->model('core/player_character');
		$this->load->model('rettighed_model');
		$this->load->model('rettighed/rollespiller');
		$this->load->model('translation/array_model');
		$this->load->model('struktur_model');
		$this->load->model('indhold/beskrivelse');

		//posts 
			//soeg
		if($this->input->post('soeg'))
		{
			$this->load->model('soeg/soeg_model');
			$data['kriterie'] = $this->input->post('kriterie');
			$data['omraade'] = $this->input->post('omraade');
			$data['resultater'] = $this->soeg_model->soeg($data['kriterie'], $data['omraade']);		
		}

		//henter deltageroplysninger
		$data['deltager'] = $this->session->userdata('deltager');
		if(empty($data['deltager'])){ redirect('login'); } //redirect til login.
		$data['ingame'] = $this->session->userdata('ingame');

		// sidehoved og sidefod
		$data['overskrift_left'] = "Du er nu ingame";
		$data['overskrift_right'] = "Meld dig til rollespil";
		$data['underskrift_left'] = "";
		$data['underskrift_right'] = "";

		// bogmærker?		
		$data['vis_bogmaerker'] = "nej";
		
		// henter fordelingsview
		$this->load->view('admin/beskrivelser',$data);

	}



    function aktiver() {

        $deltager_id = $this->uri->segment(3);
		$this->load->model('rettighed/side_administrator');
        $this->side_administrator->aktiver_rollespiller($deltager_id);  
    
    	redirect("forord/");

    }

    function deaktiver() {
       $deltager_id = $this->uri->segment(3);
		$this->load->model('rettighed/side_administrator');
        $this->side_administrator->deaktiver_rollespiller($deltager_id);

    	redirect("forord/");

    }

	function is_logged_in()
	{
		// Definerer session med $is_logged_in
		$deltager = $this->session->userdata('deltager');
		$ingame = $this->session->userdata('ingame');


			//echo "deltager er "; print_r($deltager);
			//echo "ingame er "; print_r($ingame);

		if(empty($deltager) or empty($ingame) or $ingame['ingame_id'] != -1)
		{



            $redirect['url'] = $this->uri->uri_string();
            $redirect['side'] = $this->uri->segment(5);
	    	$this->session->set_userdata('redirect', $redirect);      
			// Viser header, tilskuer.php med besked om "at det ikke lykkedes at blive deltager" og footer 
            

			redirect('login');
		
		 		
		}
	}

}
?>
